package com.herench.bs.handler;

import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicAppointmentRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * cleverClinic
 * com.herench.bs.handler
 *
 * @author wangwenkai
 * @date 2017/12/24/15:29
 */
@Component
@Handler(code = "100738" ,name="挂号支付成功通知")
public class PayConfirmForRegistAndAppointHandler extends AbstractMessageHandler {
    @Autowired
    private IClinicAppointmentRecordDao iClinicAppointmentRecordDao;

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String recordId = requestMessage.getArgsPropertyStringValue("recordId");
        Assert.notNull(recordId, "recordId 不可为空！");
        String orderId = requestMessage.getArgsPropertyStringValue("orderId");
        Assert.notNull(orderId, "orderId 不可为空！");
        String userPersonId = requestMessage.getArgsPropertyStringValue("userPersonId");
        Assert.notNull(userPersonId, "userPersonId 不可为空！");
        String orderFee = requestMessage.getArgsPropertyStringValue("orderFee");
        Assert.notNull(orderFee, "orderFee 不可为空！");
        String payTime = requestMessage.getArgsPropertyStringValue("payTime");
        Assert.notNull(payTime, "payTime 不可为空！");
        String payType = requestMessage.getArgsPropertyStringValue("payType");
        Assert.notNull(payType, "payType 不可为空！");
        String tradeNo = requestMessage.getArgsPropertyStringValue("tradeNo");
        Assert.notNull(tradeNo, "tradeNo 不可为空！");
        String openId = requestMessage.getArgsPropertyStringValue("openId");
        Assert.notNull(openId, "openId 不可为空！");
        Map<String, Object> res = new HashMap<>(16);
        Map<String, Object> params = new HashMap<>(16);
        params.put("id", recordId);
        Map<String, Object> resFindOne = iClinicAppointmentRecordDao.findOne(params);
        if (resFindOne != null) {
            String idCard = (String) resFindOne.get("idCard");
            String appointmentType = (String) resFindOne.get("appointmentType");
            IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
            String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
            Map<String, Object> resPatientId = outpatientClinicAgent.queryPatientInfo(channelCode, hospitalCode, idCard);
            String patientId = (String) resPatientId.get("patientId");
            if (!(patientId == null || "".equals(patientId))) {
                if ("1".equals(appointmentType)) {
                    //"1"预约挂号
                    res = outpatientClinicAgent.payConfirmForAppointment(channelCode, hospitalCode, recordId, openId, orderId, patientId, userPersonId, orderFee, payTime, payType, tradeNo);
                } else if ("3".equals(appointmentType)) {
                    //"3"当日挂号
                    res = outpatientClinicAgent.payConfirmForRegister(channelCode, hospitalCode, recordId, openId, orderId, patientId, userPersonId, orderFee, payTime, payType, tradeNo);
                }
            } else {
                res = resPatientId;
            }
        } else {
            res.put(ResponseMessage.RES_CODE, "-3");
            res.put(ResponseMessage.MESSAGE, "未找到相关记录");
            res.put(ResponseMessage.MESSAGE_IN, "未找到相关记录");
            res.put(ResponseMessage.MESSAGE_OUT, "未找到相关记录");
        }
        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}

